package com.yaDuo.common.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;

import androidx.annotation.NonNull;

/**
 * 无说明
 *
 * @author xiaolin
 * 2021/08/23
 */
public class GlobalContext {

    @SuppressLint("StaticFieldLeak")
    private static Context mContext;
    @SuppressLint("StaticFieldLeak")
    private static Context mModifiedContext;
    @SuppressLint("StaticFieldLeak")
    private static Context mTempContext;

    public static void set(Context context) {
        mContext = context.getApplicationContext();
        updateConfig(mContext);
    }

    private static void updateConfig(Context originContext) {
        Resources newRes = new Resources(originContext.getAssets(),
                originContext.getResources().getDisplayMetrics(),
                originContext.getResources().getConfiguration());
        mModifiedContext = new UiContext(originContext, android.R.style.Theme, originContext.getClassLoader(), newRes);
        mTempContext = new UiContext(originContext, android.R.style.Theme, originContext.getClassLoader(), newRes);

        // 修改dpi
        DpiUtils.applyDpi(originContext, mModifiedContext);
    }

    @NonNull
    public static Context get() {
        if (mContext == null) {
            throw new IllegalStateException("you have not yet initialized the sdk context !");
        }
        return mContext;
    }

    /**
     * 修改了Configuration后的Context，拿不到主题资源
     */
    @SuppressLint("NewApi")
    public static Context getModified() {
        return mModifiedContext;
    }

    /**
     * 注意有多线程问题
     *
     * @return
     */
    @SuppressLint("NewApi")
    public static Context getTemplate() {
        return mTempContext;
    }


}
